/**
 * Description:
 *
 * @author      Eden
 * @datetime    2016/12/15 14:40
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */
(function () {

    "use strict";

    var api = {
        deleteUrl: '/goods/lists/deleteGoods',
        changeStatusUrl: '/goods/lists/changeStatus'
    };

    sm.controller('admin-goos-index-controller', ['$scope', '$http', 'Modal', '$window', function ($scope, $http, Modal, $window) {

        var normal = '<span class="badge bg-green">正常</span>';
        var down = '<span class="badge bg-red">下架</span>';

        // 修改状态
        $scope.changeStatus = function (goodsId, status) {
            if (!goodsId) {
                return false;
            }

            var obj = $('#item_' + goodsId);
            if (obj.length == 0) {
                return false;
            }

            doChange(goodsId, status, obj);
        };

        /**
         * 修改状态
         * @param goodsId
         * @param obj
         */
        function doChange(goodsId, status, obj) {
            $http.get(api.changeStatusUrl, {
                params: {
                    goodsId: goodsId,
                    option: status ? 'down' : 'up'
                }
            }).success(function (res) {
                if (res.state) {
                    // 更新后的状态
                    Modal.success({
                        'title': '状态修改成功',
                        'buttons': [{
                            'name': '确定',
                            'event': function () {
                                afterChangeSuc(obj, goodsId, status);
                            }
                        }]
                    });
                } else {
                    var err = res.error || '状态修改失败';
                    Modal.error(err);
                }
            });
        }

        // 修改之后
        function afterChangeSuc(obj, goodsId, status) {
            var sObj = $('#status_' + goodsId);
            if (status) {
                obj.html(down);
                sObj.html('上架');
            } else {
                obj.html(normal);
                sObj.html('下架');
            }
        }

        // 删除
        $scope.deleteItem = function (goodsId) {
            if (!goodsId) {
                return false;
            }
            Modal.alert({
                'title': "您确定删除该商品吗?",
                'content': "删除之后不能恢复,请谨慎操作.",
                'buttons': [{
                    'name': "取消",
                    'type': 'cancel'
                }, {
                    'name': "确定删除",
                    'event': function () {
                        doDelete(goodsId);
                    }
                }]
            });
        };

        function doDelete(goodsId) {

            $http.get(api.deleteUrl, {
                params: {
                    goodsId: goodsId
                }
            }).success(function (res) {
                if (res.state) {
                    $window.location.reload();
                    // reloadPage();
                } else {
                    var err = res.error ? res.error : '删除失败';
                    Modal.error();
                }

            });

        }

        // 是否需要模态框
        function reloadPage() {
            Modal.success({
                'title': '删除成功',
                'buttons': [{
                    'name': '确定',
                    'event': function () {
                        console.log('hehre');
                        // $location.replace();
                        $window.location.reload();
                    }
                }]
            });

        }



    }]);

})();